/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.util.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.zoundry.gaedt.core.util.ZGaeCloser;
import org.zoundry.gaedt.core.util.ZGaeFileUtil;
import org.zoundry.gaedt.core.util.ZGaeUtil;

/**
 * Unpacks a .zip file to a target destination directory.
 */
public class ZGaeZipUnpacker
{
    /** The optional listener. */
    private IZGaeZipUnpackListener mListener;

    /**
     * C'tor.
     */
    public ZGaeZipUnpacker()
    {
    }

    /**
     * C'tor.
     *
     * @param aListener
     */
    public ZGaeZipUnpacker(IZGaeZipUnpackListener aListener)
    {
        setListener(aListener);
    }

    /**
     * Unpacks the contents of a given ZIP file to the given target directory.
     *
     * @param aFile The source ZIP file.
     * @param aTargetDir The target directory to unpack into.
     * @throws IOException
     */
    public void unpack(File aFile, File aTargetDir) throws IOException
    {
        File rootDir = aTargetDir;
        ZGaeFileUtil.makeDirs(rootDir);

        ZipFile zipFile = new ZipFile(aFile);
        int numEntries = zipFile.size();
        zipFile.close();

        if (getListener() != null)
            getListener().onZipUnpackStarted(numEntries);

        ZipInputStream in = null;
        try
        {
            in = new ZipInputStream(new FileInputStream(aFile));
            ZipEntry entry = in.getNextEntry();
            while (entry != null)
            {
                FileOutputStream out = null;
                try
                {
                    if (!entry.isDirectory())
                    {
                        File newFile = new File(rootDir, entry.getName());
                        ZGaeFileUtil.makeDirs(newFile.getParentFile());
                        out = new FileOutputStream(newFile);
                        copyEntry(in, out);
                    }
                    if (getListener() != null)
                        getListener().onZipEntryUnpacked(entry.getName());
                    entry = in.getNextEntry();
                }
                catch (IOException io)
                {
                    ZGaeUtil.logError(io);
                    throw io;
                }
                finally
                {
                    ZGaeCloser.close(out);
                }
            }
        }
        finally
        {
            ZGaeCloser.close(in);
            if (getListener() != null)
                getListener().onZipUnpackCompleted();
        }
    }

    /**
     * Copies a given ZIP entry to the given output stream.
     *
     * @param aIn The ZIP input stream.
     * @param aOut The output stream to write to.
     * @throws IOException
     */
    protected void copyEntry(ZipInputStream aIn, OutputStream aOut) throws IOException
    {
        try
        {
            ZGaeFileUtil.copy(aIn, aOut);
        }
        finally
        {
            aIn.closeEntry();
        }
    }

    /**
     * @return the listener
     */
    protected IZGaeZipUnpackListener getListener()
    {
        return mListener;
    }

    /**
     * @param aListener the listener to set
     */
    public void setListener(IZGaeZipUnpackListener aListener)
    {
        mListener = aListener;
    }
}
